<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<title>添加备件</title>
		<!-- css代码 start -->
		<link href="../../../../css/mui.min.css" rel="stylesheet" />
		<link href="../../../../css/global.css" rel="stylesheet" />
		<link href="../../../../css/style.css" rel="stylesheet" />
		<link rel="stylesheet" href="../../../../css/mui.picker.css">
		<link rel="stylesheet" href="../../../../css/mui.poppicker.css">
		<style type="text/css">
			.mui-btn-block{
				border: 0;
				font-size: 16px;
				color: #a9a9a9;
			}
			input.d_ipt{
				width: 60%;
				padding: 10px 0;
			}
		</style>
		<!-- css代码 end -->
	</head>

	<body>
		<!-- html代码 start -->
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 id="title" class="mui-title">添加备件</h1>
		</header>
		<div class="mui-content">
			<div class="details_con">
				<div hidden>
					<input type="hidden" id="id" />
					<input type="hidden" id="serviceNo" value="1"/>
				</div>
				<div class="clum">
					<span class="name">服务编号</span>
					<span class="txt" id="serviceNoText"></span>
				</div>
				<div class="clum">
					<span class="name">备件编码<em class="t_red">*</em></span>
					<input type="text" maxlength="50" id="partNumber" class="d_ipt" placeholder="请输入备件编码" />
				</div>
				<!-- <div class="clum">
					<span class="name">备件类型</span>
					<span class="txt">11700001</span>
				</div> -->
				<div class="clum">
					<span class="name">备件类型<em class="t_red">*</em></span>
					<input type="hidden" id="partType" />
					<span class="txt">
						<button id='partTypeName' class="mui-btn mui-btn-block mui-btn-select" type='button'>请选择</button>
					</span>
				</div>
				<div class="clum">
					<span class="name">备件数量<em class="t_red">*</em></span>
					<input type="number" id="qtyCount" class="d_ipt" placeholder="请输入备件数量" oninput = "qtyCountCheck(this)"/>
				</div>
				<div class="clum">
					<span class="name">备件名称<em class="t_red">*</em></span>
					<input type="text" maxlength="250" id="partName" class="d_ipt" placeholder="请输入备件名称" />
				</div>
				<div class="clum">
					<span class="name">备件序列号</span>
					<input type="text" maxlength="50" id="partSerialNumber" class="d_ipt" placeholder="请输入备件序列号" />
				</div>
				<div class="clum">
					<span class="name">备件收货地址<em class="t_red">*</em></span>
					<input type="text" maxlength="250" id="address" class="d_ipt" placeholder="请输入备件收货地址" />
				</div>
				<div class="clum">
					<span class="name">借货单号</span>
					<input type="text" maxlength="50" id="loanNumber" class="d_ipt" placeholder="请输入借货单号" />
				</div>
			</div>
			<div class="btn_box">
				<span class="fl" id="cancle">取消</span>
				<span class="fr red" id="saveParts">保存</span>
			</div>
			<div style="height: 50px;"></div>
		</div>
		<!-- html代码 end -->
		
		<!-- js代码 start -->
		
		<!-- 基础js start -->
		<script src="../../../../js/mui.min.js"></script>
		<script src="../../../../js/utils/h.js"></script>
		<script src="../../../../js/constant.js"></script>
		<script src="../../../../js/common.js"></script>
		<script src="../../../../js/utils/base64.js"></script>
		<script src="../../../../js/utils/md5.js"></script>
		<script src="../../../../js/utils/sortMap.js"></script>
		<script src="../../../../js/utils/http.js"></script>
		<script src="../../../../js/app.js"></script>
		<!-- 基础js end -->
		<script src="../../../../js/mui.picker.js"></script>
		<script src="../../../../js/mui.poppicker.js"></script>
		<script type="text/javascript" charset="utf-8">
			var portType
			var helpId
			var annexHelpId
			
			function qtyCountCheck(obj) {
				obj.value=obj.value.replace(/[^\d]/g,'')
				if(obj.value.length>8)obj.value=obj.value.slice(0,8)
			}
			
			(function($, doc) {
				$.init();
		
				$.plusReady(function() {
					var self = plus.webview.currentWebview();
					// 获取上一个页面传过来的参数并显示到页面
					h('#serviceNo').val(self.serviceNo);
					h('#serviceNoText').html(self.serviceNo);
					portType = self.portType
					helpId = self.helpId
					annexHelpId = new Date().UUID(self.serviceNo);
					// 保存按钮触发事件
					h('#saveParts').tap(function() {
						app.hideSoftKeybord()
						app.showWaiting('正在保存...')
						savePart()
					})
					
					var savePart = function(){
						var partNumber = h("#partNumber").val();
						if(partNumber==null || partNumber==""){
							app.closeWaiting()
							app.toast("请填写备件编码");
							return
						}
						var qtyCount = h("#qtyCount").val();
						if(qtyCount==null || qtyCount==""){
							app.closeWaiting()
							app.toast("请填写备件数量");
							return
						}
						
						var partName = h("#partName").val();
						if(partName==null || partName==""){
							app.closeWaiting()
							app.toast("请填写备件名称");
							return
						}
						var address = h("#address").val();
						if(address==null || address==""){
							app.closeWaiting()
							app.toast("请填写备件收货地址");
							return
						}
						
						var partType = h('#partType').val();
						if (partType==null || partType=="") {
							partType = ""
						}
						
						var partTypeName = h('#partTypeName').html();
						if (!partTypeName || partTypeName == "请选择") {
							partTypeName = ""
						}
						if(!partTypeName){
							app.closeWaiting()
							app.toast('请填写备件类型')
							return
						}

						if (!app.getNetworkState()) {
							app.closeWaiting()
							app.toast("无网络")
							return
						}
						
						
						
						var params = {
							serviceNo: self.serviceNo,
							enginnerId: app.getUser().enginnerId,
							pid: "",
							partNumber : partNumber,
							portType : portType,
							partType : partType,
							partTypeName : partTypeName,
							qtyCount : qtyCount,
							partName : partName,
							partSerialNumber : h('#partSerialNumber').val(),
							address : address,
							loanNumber : h('#loanNumber').val(),
							helpId : helpId,
							annexHelpId : annexHelpId
						} 
						console.log(JSON.stringify(params))
						ajaxPost(BASE_URL + "cc/report/port/addReportPort", params, function(data) {
							app.closeWaiting()
							if (data.code === 200) {
								app.toast("保存成功")
								var sparePartsList = plus.webview.getWebviewById('view/service/report/errorhot/sparePartsList.html');
								if(sparePartsList){
									sparePartsList.evalJS('initData()')
								}
								self.close()
							} else {
								app.toast(data.msg)
							}
						}, function(errorMsg) {
							app.closeWaiting()
							app.toast(errorMsg)
						});
					}
					
					// 备件类型
					h('#partTypeName').tap(function() {
						app.hideSoftKeybord()
						var userPicker = new $.PopPicker();
						ajaxGetJson(BASE_URL + 'cc/dict/selectDict', {
							dictType: 'parttype_report'
						}, function(data){
							console.log(JSON.stringify(data))
							if (!data || data.length <= 0) {
								app.toast("无备件类型数据")
								return
							}							
							
							userPicker.setData(data);
							userPicker.show(function(items) {
								console.log(items);
								h('#partTypeName').html(items[0].text)
								h('#partType').val(items[0].value)
								userPicker.dispose()
							});						
						});
											
						// var data = [{"text":"请选择","value":""},{"text":"漏发","value":"2"},{"text":"错发","value":"3"}]
						
						// userPicker.setData(data);
						// userPicker.show(function(items) {
						// 	h('#partTypeName').html(items[0].text)
						// 	h('#partType').val(items[0].value)
						// 	userPicker.dispose()
						// });
						// [{"text":"更换","value":"1"},{"text":"漏发","value":"2"},{"text":"错发","value":"3"},{"text":"多发","value":"4"},{"text":"个人工具","value":"5"}]
						// ajaxGetJson(BASE_URL + 'cc/dict/selectCCDict', {
						// 	tableName: 'PartType',
						// 	engineerId: app.getUser().enginnerId
						// }, function(data) {
						// 	console.log(JSON.stringify(data))
						// 	if (!data) {
						// 		app.toast("无备件类型数据")
						// 		return
						// 	}
							
						// });
					});
					
					h('#cancle').tap(function() {
						$.back()
					})
					
				});
			}(mui, document));
		</script>
		<!-- js代码 end -->
	</body>

</html>
